<?php
class MHDEV_View_Helper_HasAccess extends Zend_View_Helper_Abstract
{
    public function HasAccess($board_uid)
    {
    	// var
    	$status			= false;
    	
		// db
		$user 			= new User();
		$groups			= new Groups();
		$permissions 	= new Permissions();
		
		// auth
		$auth			= Zend_Auth::getInstance();
		
		$user_uid		= $auth->hasIdentity() ? $auth->getIdentity()->uid : 0;
		
		if($user_uid > 0)
		{
			// select
			$select			= $user->select()->setIntegrityCheck(false);
			$select->from(
					array('u' => 'user'), array('u.group_uid', 'u.role')
				)
				->join(
					array('g' => 'groups'), 'u.group_uid = g.uid', array('g.perm_uid')
				)
				->join(
					array('p' => 'permissions'), 'g.perm_uid = p.perm_uid', array('has_access')
				)
				->where('u.uid = ?', $user_uid)
				->where('u.is_active = ?', 1)
				->where('p.board_uid = ?', $board_uid);
			
			// query
			$query	= $user->fetchAll($select);
			
			if($query->count() > 0)
			{
				foreach($query->toArray() as $result)
				{
					if($result['has_access'] == 1 OR $result['role'] == 'admin')
					{
						$status = true;
					}
				}
			}
			elseif($auth->getIdentity()->role == 'admin')
			{
				$status = true;
			}
		}
		
		return $status;
    }
}
?>